If an enumerator list is given with no explicit initialization of members, then C/C++ allocates a sequence of integers starting at zero for the
first element and increasing by one for each subsequent element.
An explicit initialization of the first element, as permitted by this rule, forces the allocation of integers to start at the given value. When
adopting this approach it is essential to ensure that the initialization value used is small enough that no subsequent value in the list will exceed
the int
storage used by enumeration constants.
Explicit initialization of all items in the list, which is also permissible, prevents the mixing of automatic and manual allocation, which is error
prone.
However, it is then the responsibility of the developer to ensure that all values are in the required range, and that values are not
unintentionally duplicated.
Noncompliant code example
enum color { red = 3, blue, green, yellow = 5 }; // Noncompliant; both green and yellow = 5
Compliant solution
enum color { red = 3, blue = 4, green = 5, yellow = 5 }; // Compliant